{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Calculating stand level measurements from tree data\n",
    "\n",
    "For this example, we'll start with loading some tree data and then calculate stand trees per acre, basal area. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Method definition ==(Base.Nullable{"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>Stand_ID</th><th>Plot_ID</th><th>Tree_ID</th><th>Tree_Count</th><th>History</th><th>Species</th><th>DBH</th><th>Ht</th></tr></thead><tbody><tr><th>1</th><td>12345</td><td>1</td><td>1</td><td>1</td><td>1</td><td>WO</td><td>11</td><td>63</td></tr><tr><th>2</th><td>12345</td><td>1</td><td>2</td><td>1</td><td>1</td><td>WO</td><td>12</td><td>55</td></tr><tr><th>3</th><td>12345</td><td>1</td><td>3</td><td>1</td><td>1</td><td>WO</td><td>12</td><td>60</td></tr><tr><th>4</th><td>12345</td><td>1</td><td>4</td><td>1</td><td>1</td><td>WO</td><td>12</td><td>45</td></tr><tr><th>5</th><td>12345</td><td>1</td><td>5</td><td>1</td><td>1</td><td>WO</td><td>17</td><td>55</td></tr><tr><th>6</th><td>12345</td><td>1</td><td>6</td><td>1</td><td>1</td><td>WO</td><td>18</td><td>60</td></tr><tr><th>7</th><td>12345</td><td>1</td><td>7</td><td>1</td><td>1</td><td>WO</td><td>32</td><td>60</td></tr><tr><th>8</th><td>12345</td><td>1</td><td>8</td><td>1</td><td>1</td><td>WO</td><td>15</td><td>55</td></tr><tr><th>9</th><td>12345</td><td>1</td><td>9</td><td>1</td><td>1</td><td>WO</td><td>16</td><td>68</td></tr><tr><th>10</th><td>12345</td><td>1</td><td>10</td><td>1</td><td>1</td><td>RO</td><td>10</td><td>65</td></tr><tr><th>11</th><td>12345</td><td>2</td><td>1</td><td>1</td><td>1</td><td>WO</td><td>5</td><td>30</td></tr><tr><th>12</th><td>12345</td><td>2</td><td>2</td><td>1</td><td>1</td><td>WO</td><td>9</td><td>50</td></tr><tr><th>13</th><td>12345</td><td>3</td><td>2</td><td>1</td><td>1</td><td>WO</td><td>11</td><td>45</td></tr><tr><th>14</th><td>12345</td><td>3</td><td>3</td><td>1</td><td>1</td><td>WO</td><td>11</td><td>55</td></tr><tr><th>15</th><td>12345</td><td>3</td><td>4</td><td>1</td><td>1</td><td>WO</td><td>11</td><td>50</td></tr><tr><th>16</th><td>12345</td><td>3</td><td>5</td><td>1</td><td>1</td><td>WO</td><td>13</td><td>50</td></tr><tr><th>17</th><td>12345</td><td>3</td><td>6</td><td>1</td><td>1</td><td>WO</td><td>14</td><td>50</td></tr><tr><th>18</th><td>12345</td><td>3</td><td>7</td><td>1</td><td>1</td><td>WO</td><td>15</td><td>55</td></tr><tr><th>19</th><td>12345</td><td>3</td><td>8</td><td>1</td><td>1</td><td>WO</td><td>17</td><td>55</td></tr><tr><th>20</th><td>12345</td><td>4</td><td>7</td><td>1</td><td>1</td><td>WO</td><td>12</td><td>60</td></tr><tr><th>21</th><td>12345</td><td>4</td><td>8</td><td>1</td><td>1</td><td>WO</td><td>8</td><td>55</td></tr><tr><th>22</th><td>12345</td><td>4</td><td>9</td><td>1</td><td>1</td><td>NRO</td><td>5</td><td>45</td></tr><tr><th>23</th><td>12345</td><td>4</td><td>10</td><td>1</td><td>1</td><td>NRO</td><td>13</td><td>60</td></tr><tr><th>24</th><td>12345</td><td>5</td><td>1</td><td>1</td><td>1</td><td>WO</td><td>9</td><td>60</td></tr><tr><th>25</th><td>12345</td><td>5</td><td>2</td><td>1</td><td>1</td><td>WO</td><td>11</td><td>65</td></tr><tr><th>26</th><td>12345</td><td>5</td><td>3</td><td>1</td><td>1</td><td>WO</td><td>11</td><td>65</td></tr><tr><th>27</th><td>12345</td><td>6</td><td>1</td><td>1</td><td>1</td><td>SO</td><td>5</td><td>40</td></tr><tr><th>28</th><td>12345</td><td>6</td><td>2</td><td>1</td><td>1</td><td>WO</td><td>11</td><td>55</td></tr><tr><th>29</th><td>12345</td><td>6</td><td>3</td><td>1</td><td>1</td><td>WO</td><td>11</td><td>60</td></tr><tr><th>30</th><td>12345</td><td>6</td><td>4</td><td>1</td><td>1</td><td>WO</td><td>15</td><td>60</td></tr><tr><th>&vellip;</th><td>&vellip;</td><td>&vellip;</td><td>&vellip;</td><td>&vellip;</td><td>&vellip;</td><td>&vellip;</td><td>&vellip;</td><td>&vellip;</td></tr></tbody></table>"
      ],
      "text/plain": [
       "154×8 DataFrames.DataFrame\n",
       "│ Row │ Stand_ID │ Plot_ID │ Tree_ID │ Tree_Count │ History │ Species │ DBH │\n",
       "├─────┼──────────┼─────────┼─────────┼────────────┼─────────┼─────────┼─────┤\n",
       "│ 1   │ 12345    │ 1       │ 1       │ 1          │ 1       │ \"WO\"    │ 11  │\n",
       "│ 2   │ 12345    │ 1       │ 2       │ 1          │ 1       │ \"WO\"    │ 12  │\n",
       "│ 3   │ 12345    │ 1       │ 3       │ 1          │ 1       │ \"WO\"    │ 12  │\n",
       "│ 4   │ 12345    │ 1       │ 4       │ 1          │ 1       │ \"WO\"    │ 12  │\n",
       "│ 5   │ 12345    │ 1       │ 5       │ 1          │ 1       │ \"WO\"    │ 17  │\n",
       "│ 6   │ 12345    │ 1       │ 6       │ 1          │ 1       │ \"WO\"    │ 18  │\n",
       "│ 7   │ 12345    │ 1       │ 7       │ 1          │ 1       │ \"WO\"    │ 32  │\n",
       "│ 8   │ 12345    │ 1       │ 8       │ 1          │ 1       │ \"WO\"    │ 15  │\n",
       "│ 9   │ 12345    │ 1       │ 9       │ 1          │ 1       │ \"WO\"    │ 16  │\n",
       "│ 10  │ 12345    │ 1       │ 10      │ 1          │ 1       │ \"RO\"    │ 10  │\n",
       "│ 11  │ 12345    │ 2       │ 1       │ 1          │ 1       │ \"WO\"    │ 5   │\n",
       "⋮\n",
       "│ 143 │ 12345    │ 15      │ 6       │ 1          │ 1       │ \"WO\"    │ 7   │\n",
       "│ 144 │ 12345    │ 16      │ 2       │ 1          │ 1       │ \"WO\"    │ 19  │\n",
       "│ 145 │ 12345    │ 16      │ 3       │ 1          │ 1       │ \"WO\"    │ 18  │\n",
       "│ 146 │ 12345    │ 16      │ 4       │ 1          │ 1       │ \"WO\"    │ 18  │\n",
       "│ 147 │ 12345    │ 16      │ 5       │ 1          │ 1       │ \"NRO\"   │ 10  │\n",
       "│ 148 │ 12345    │ 16      │ 6       │ 1          │ 1       │ \"NRO\"   │ 11  │\n",
       "│ 149 │ 12345    │ 16      │ 7       │ 1          │ 1       │ \"NRO\"   │ 9   │\n",
       "│ 150 │ 12345    │ 17      │ 1       │ 1          │ 1       │ \"SO\"    │ 13  │\n",
       "│ 151 │ 12345    │ 17      │ 9       │ 1          │ 1       │ \"BO\"    │ 16  │\n",
       "│ 152 │ 12345    │ 17      │ 10      │ 1          │ 1       │ \"BO\"    │ 16  │\n",
       "│ 153 │ 12345    │ 17      │ 11      │ 1          │ 1       │ \"BO\"    │ 18  │\n",
       "│ 154 │ 12345    │ 17      │ 12      │ 1          │ 1       │ \"BO\"    │ 21  │\n",
       "\n",
       "│ Row │ Ht │\n",
       "├─────┼────┤\n",
       "│ 1   │ 63 │\n",
       "│ 2   │ 55 │\n",
       "│ 3   │ 60 │\n",
       "│ 4   │ 45 │\n",
       "│ 5   │ 55 │\n",
       "│ 6   │ 60 │\n",
       "│ 7   │ 60 │\n",
       "│ 8   │ 55 │\n",
       "│ 9   │ 68 │\n",
       "│ 10  │ 65 │\n",
       "│ 11  │ 30 │\n",
       "⋮\n",
       "│ 143 │ 55 │\n",
       "│ 144 │ 63 │\n",
       "│ 145 │ 63 │\n",
       "│ 146 │ 55 │\n",
       "│ 147 │ 66 │\n",
       "│ 148 │ 64 │\n",
       "│ 149 │ 58 │\n",
       "│ 150 │ 50 │\n",
       "│ 151 │ 50 │\n",
       "│ 152 │ 60 │\n",
       "│ 153 │ 65 │\n",
       "│ 154 │ 55 │"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Load packages\n",
    "# Pkg.add(\"ForestBiometrics) # if install needed\n",
    "using ForestBiometrics, CSV, DataFrames\n",
    "\n",
    "#Load data\n",
    "datapath = Pkg.dir(\"ForestBiometrics\", \"test\", \"data\")\n",
    "df=CSV.read(joinpath(datapath, \"StandExam_data.csv\"),nullable=false)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is the tree data for a single stand with 17 plots taken. To simplify, a bit we'll just explain the specs here and not have to worry about a second table with cruise spec info. The plots were variable radius, measured with a BAF 10 prism.\n",
    "\n",
    "ForestBiometrics exports a CONST ```K``` for the conversion factor between diameter in inches and area of a circle, as well as the metric version ```KMETRIC``` for diameter in centimeters.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "S}, Base.Nullable{T}) in module Base at nullable.jl:238 overwritten in module NullableArrays at C:\\Users\\Casey\\.julia\\v0.6\\NullableArrays\\src\\operators.jl:99.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.005454154"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "K"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.854e-5"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "KMETRIC"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Basal Area\n",
    "\n",
    "First let's start with calculating the basal area in ft^2 for each plot, so we need to get numbers of records/plot * 10 to get BA represented by that plot. Then we can generate our stand estimate for basal area/acre by taking the average of all plots in the stand."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>Plot_ID</th><th>BA</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>100</td></tr><tr><th>2</th><td>2</td><td>80</td></tr><tr><th>3</th><td>3</td><td>90</td></tr><tr><th>4</th><td>4</td><td>100</td></tr><tr><th>5</th><td>5</td><td>100</td></tr><tr><th>6</th><td>6</td><td>100</td></tr><tr><th>7</th><td>7</td><td>110</td></tr><tr><th>8</th><td>8</td><td>70</td></tr><tr><th>9</th><td>9</td><td>80</td></tr><tr><th>10</th><td>10</td><td>110</td></tr><tr><th>11</th><td>11</td><td>110</td></tr><tr><th>12</th><td>12</td><td>30</td></tr><tr><th>13</th><td>13</td><td>80</td></tr><tr><th>14</th><td>14</td><td>70</td></tr><tr><th>15</th><td>15</td><td>120</td></tr><tr><th>16</th><td>16</td><td>70</td></tr><tr><th>17</th><td>17</td><td>120</td></tr></tbody></table>"
      ],
      "text/plain": [
       "17×2 DataFrames.DataFrame\n",
       "│ Row │ Plot_ID │ BA  │\n",
       "├─────┼─────────┼─────┤\n",
       "│ 1   │ 1       │ 100 │\n",
       "│ 2   │ 2       │ 80  │\n",
       "│ 3   │ 3       │ 90  │\n",
       "│ 4   │ 4       │ 100 │\n",
       "│ 5   │ 5       │ 100 │\n",
       "│ 6   │ 6       │ 100 │\n",
       "│ 7   │ 7       │ 110 │\n",
       "│ 8   │ 8       │ 70  │\n",
       "│ 9   │ 9       │ 80  │\n",
       "│ 10  │ 10      │ 110 │\n",
       "│ 11  │ 11      │ 110 │\n",
       "│ 12  │ 12      │ 30  │\n",
       "│ 13  │ 13      │ 80  │\n",
       "│ 14  │ 14      │ 70  │\n",
       "│ 15  │ 15      │ 120 │\n",
       "│ 16  │ 16      │ 70  │\n",
       "│ 17  │ 17      │ 120 │"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#make some variables, so we can reference it for simplicity\n",
    "BAF = 10\n",
    "n_plots=17\n",
    "\n",
    "plot_ba=by(df,:Plot_ID,df -> DataFrame(BA = size(df, 1)*BAF))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "90.58823529411765"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stand_ba=mean(plot_ba[:BA])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Trees per acre\n",
    "\n",
    "TPA for variable radius plots is a little more involved, but nothing we can't handle. \n",
    "First, we'll calculate the appropriate expansion factor for each tree record and them combine them to get our estimate for the stand."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "147.85936586694856"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Julia uses dots before operators (./, .+,.^) to apply functions element-wise in an array. \n",
    "\n",
    "#calculate basal area of each tree record\n",
    "df[:TreeBA]=(df[:DBH].^2)*K\n",
    "\n",
    "#calculating expansion factor for each tree record\n",
    "df[:TreeEXP]=BAF./df[:TreeBA]./n_plots\n",
    "\n",
    "# sum together to get stand estimate \n",
    "stand_tpa=sum(df[:TreeEXP])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "Since, we are dealing with upland oak species, let's see where this stand falls on a gingrich chart. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\" viewBox=\"0 0 600 400\">\n",
       "<defs>\n",
       "  <clipPath id=\"clip6900\">\n",
       "    <rect x=\"0\" y=\"0\" width=\"600\" height=\"400\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polygon clip-path=\"url(#clip6900)\" points=\"\n",
       "0,400 600,400 600,0 0,0 \n",
       "  \" fill=\"#ffffff\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip6901\">\n",
       "    <rect x=\"120\" y=\"0\" width=\"421\" height=\"400\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polygon clip-path=\"url(#clip6900)\" points=\"\n",
       "51.265,360.065 580.315,360.065 580.315,11.811 51.265,11.811 \n",
       "  \" fill=\"#ffffff\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip6902\">\n",
       "    <rect x=\"51\" y=\"11\" width=\"530\" height=\"349\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  53.3458,360.065 53.3458,11.811 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  114.207,360.065 114.207,11.811 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  175.067,360.065 175.067,11.811 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  235.928,360.065 235.928,11.811 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  296.789,360.065 296.789,11.811 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  357.649,360.065 357.649,11.811 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  418.51,360.065 418.51,11.811 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  479.371,360.065 479.371,11.811 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  540.232,360.065 540.232,11.811 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  51.265,351.291 580.315,351.291 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  51.265,302.012 580.315,302.012 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  51.265,252.733 580.315,252.733 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  51.265,203.455 580.315,203.455 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  51.265,154.176 580.315,154.176 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  51.265,104.897 580.315,104.897 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  51.265,55.6184 580.315,55.6184 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6900)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  51.265,360.065 580.315,360.065 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6900)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  51.265,360.065 51.265,11.811 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6900)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  53.3458,360.065 53.3458,354.842 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6900)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  114.207,360.065 114.207,354.842 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6900)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  175.067,360.065 175.067,354.842 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6900)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  235.928,360.065 235.928,354.842 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6900)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  296.789,360.065 296.789,354.842 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6900)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  357.649,360.065 357.649,354.842 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6900)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  418.51,360.065 418.51,354.842 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6900)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  479.371,360.065 479.371,354.842 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6900)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  540.232,360.065 540.232,354.842 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6900)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  51.265,351.291 59.2007,351.291 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6900)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  51.265,302.012 59.2007,302.012 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6900)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  51.265,252.733 59.2007,252.733 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6900)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  51.265,203.455 59.2007,203.455 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6900)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  51.265,154.176 59.2007,154.176 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6900)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  51.265,104.897 59.2007,104.897 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6900)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  51.265,55.6184 59.2007,55.6184 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:middle;\" transform=\"rotate(0, 53.3458, 373.865)\" x=\"53.3458\" y=\"373.865\">0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:middle;\" transform=\"rotate(0, 114.207, 373.865)\" x=\"114.207\" y=\"373.865\">50</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:middle;\" transform=\"rotate(0, 175.067, 373.865)\" x=\"175.067\" y=\"373.865\">100</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:middle;\" transform=\"rotate(0, 235.928, 373.865)\" x=\"235.928\" y=\"373.865\">150</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:middle;\" transform=\"rotate(0, 296.789, 373.865)\" x=\"296.789\" y=\"373.865\">200</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:middle;\" transform=\"rotate(0, 357.649, 373.865)\" x=\"357.649\" y=\"373.865\">250</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:middle;\" transform=\"rotate(0, 418.51, 373.865)\" x=\"418.51\" y=\"373.865\">300</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:middle;\" transform=\"rotate(0, 479.371, 373.865)\" x=\"479.371\" y=\"373.865\">350</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:middle;\" transform=\"rotate(0, 540.232, 373.865)\" x=\"540.232\" y=\"373.865\">400</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:end;\" transform=\"rotate(0, 45.265, 355.791)\" x=\"45.265\" y=\"355.791\">20</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:end;\" transform=\"rotate(0, 45.265, 306.512)\" x=\"45.265\" y=\"306.512\">40</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:end;\" transform=\"rotate(0, 45.265, 257.233)\" x=\"45.265\" y=\"257.233\">60</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:end;\" transform=\"rotate(0, 45.265, 207.955)\" x=\"45.265\" y=\"207.955\">80</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:end;\" transform=\"rotate(0, 45.265, 158.676)\" x=\"45.265\" y=\"158.676\">100</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:end;\" transform=\"rotate(0, 45.265, 109.397)\" x=\"45.265\" y=\"109.397\">120</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:end;\" transform=\"rotate(0, 45.265, 60.1184)\" x=\"45.265\" y=\"60.1184\">140</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:16; text-anchor:middle;\" transform=\"rotate(0, 315.79, 397.6)\" x=\"315.79\" y=\"397.6\">Trees Per Acre</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:16; text-anchor:middle;\" transform=\"rotate(-90, 14.4, 185.938)\" x=\"14.4\" y=\"185.938\">Basal Area (sq ft./acre)</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  146.436,350.209 192.981,325.029 239.526,299.849 286.071,274.668 332.616,249.488 379.161,224.308 425.707,199.128 472.252,173.947 518.797,148.767 565.342,123.587 \n",
       "  \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  128.117,347.737 165.502,321.321 202.888,294.904 240.273,268.488 277.659,242.071 315.044,215.655 352.43,189.239 389.815,162.822 427.201,136.406 464.587,109.989 \n",
       "  \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  104.841,343.716 130.589,315.289 156.336,286.863 182.084,258.436 207.832,230.009 233.579,201.582 259.327,173.155 285.075,144.729 310.822,116.302 336.57,87.875 \n",
       "  \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  91.0731,340.589 109.937,310.599 128.8,280.609 147.664,250.619 166.528,220.629 185.391,190.639 204.255,160.649 223.118,130.659 241.982,100.669 260.846,70.6788 \n",
       "  \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  82.2187,338.09 96.6551,306.851 111.092,275.611 125.528,244.371 139.964,213.132 154.401,181.892 168.837,150.652 183.274,119.413 197.71,88.173 212.147,56.9333 \n",
       "  \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  76.1743,336.048 87.5886,303.787 99.0029,271.526 110.417,239.265 121.831,207.003 133.246,174.742 144.66,142.481 156.074,110.22 167.489,77.9594 178.903,45.6984 \n",
       "  \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  71.8586,334.347 81.115,301.236 90.3713,268.124 99.6277,235.013 108.884,201.902 118.14,168.79 127.397,135.679 136.653,102.568 145.91,69.4566 155.166,36.3452 \n",
       "  \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  68.6667,332.909 76.3271,299.079 83.9875,265.249 91.648,231.419 99.3084,197.589 106.969,163.759 114.629,129.929 122.29,96.0988 129.95,62.2687 137.611,28.4386 \n",
       "  \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  66.2381,331.678 72.6842,297.233 79.1303,262.787 85.5765,228.341 92.0226,193.896 98.4687,159.45 104.915,125.004 111.361,90.5586 117.807,56.1129 124.253,21.6673 \n",
       "  \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  146.436,350.209 128.117,347.737 104.841,343.716 91.0731,340.589 82.2187,338.09 76.1743,336.048 71.8586,334.347 68.6667,332.909 66.2381,331.678 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  192.981,325.029 165.502,321.321 130.589,315.289 109.937,310.599 96.6551,306.851 87.5886,303.787 81.115,301.236 76.3271,299.079 72.6842,297.233 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  239.526,299.849 202.888,294.904 156.336,286.863 128.8,280.609 111.092,275.611 99.0029,271.526 90.3713,268.124 83.9875,265.249 79.1303,262.787 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  286.071,274.668 240.273,268.488 182.084,258.436 147.664,250.619 125.528,244.371 110.417,239.265 99.6277,235.013 91.648,231.419 85.5765,228.341 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  332.616,249.488 277.659,242.071 207.832,230.009 166.528,220.629 139.964,213.132 121.831,207.003 108.884,201.902 99.3084,197.589 92.0226,193.896 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  379.161,224.308 315.044,215.655 233.579,201.582 185.391,190.639 154.401,181.892 133.246,174.742 118.14,168.79 106.969,163.759 98.4687,159.45 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  425.707,199.128 352.43,189.239 259.327,173.155 204.255,160.649 168.837,150.652 144.66,142.481 127.397,135.679 114.629,129.929 104.915,125.004 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  472.252,173.947 389.815,162.822 285.075,144.729 223.118,130.659 183.274,119.413 156.074,110.22 136.653,102.568 122.29,96.0988 111.361,90.5586 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  518.797,148.767 427.201,136.406 310.822,116.302 241.982,100.669 197.71,88.173 167.489,77.9594 145.91,69.4566 129.95,62.2687 117.807,56.1129 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  565.342,123.587 464.587,109.989 336.57,87.875 260.846,70.6788 212.147,56.9333 178.903,45.6984 155.166,36.3452 137.611,28.4386 124.253,21.6673 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#ff0000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  518.797,148.767 427.201,136.406 310.822,116.302 241.982,100.669 197.71,88.173 167.489,77.9594 145.91,69.4566 129.95,62.2687 117.807,56.1129 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6902)\" style=\"stroke:#ff0000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
       "  326.377,252.864 272.363,245.814 203.313,234.998 162.459,227.097 136.294,221.075 118.53,216.333 105.92,212.504 96.6458,209.348 89.6263,206.701 \n",
       "  \"/>\n",
       "<circle clip-path=\"url(#clip6902)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"233.322\" cy=\"177.366\" r=\"4\"/>\n",
       "<circle clip-path=\"url(#clip6902)\" style=\"fill:#47a065; stroke:none; fill-opacity:1\" cx=\"233.322\" cy=\"177.366\" r=\"3\"/>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:15; text-anchor:middle;\" transform=\"rotate(0, 175.067, 359.255)\" x=\"175.067\" y=\"359.255\">20%</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:15; text-anchor:middle;\" transform=\"rotate(0, 221.321, 334.615)\" x=\"221.321\" y=\"334.615\">30%</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:15; text-anchor:middle;\" transform=\"rotate(0, 265.141, 312.44)\" x=\"265.141\" y=\"312.44\">40%</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:15; text-anchor:middle;\" transform=\"rotate(0, 313.83, 285.337)\" x=\"313.83\" y=\"285.337\">50%</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:15; text-anchor:middle;\" transform=\"rotate(0, 362.518, 258.233)\" x=\"362.518\" y=\"258.233\">60%</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:15; text-anchor:middle;\" transform=\"rotate(0, 406.338, 236.058)\" x=\"406.338\" y=\"236.058\">70%</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:15; text-anchor:middle;\" transform=\"rotate(0, 447.723, 211.419)\" x=\"447.723\" y=\"211.419\">80%</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:15; text-anchor:middle;\" transform=\"rotate(0, 496.412, 186.779)\" x=\"496.412\" y=\"186.779\">90%</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:15; text-anchor:middle;\" transform=\"rotate(0, 545.1, 162.14)\" x=\"545.1\" y=\"162.14\">100%</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:15; text-anchor:middle;\" transform=\"rotate(0, 588.92, 130.109)\" x=\"588.92\" y=\"130.109\">110%</text>\n",
       "</g>\n",
       "</svg>\n"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gingrich_chart(stand_tpa,stand_ba)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It looks like the stand is appropriately stocked, sitting just below 80%. Let's see what the QMD of the stand is."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10.598582432435709"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#this function will be added to the next release, but I forgot to add it in previously.\n",
    "function qmd(ba, tpa, constant)\n",
    "    qmd=sqrt((ba/tpa)/constant)\n",
    "end\n",
    "\n",
    "stand_qmd=qmd(stand_ba,stand_tpa,K)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since the stand is not fully competing yet and QMD is just starting to get into sawtimber range, a landowner may decide to let this stand continue to develop for a few more years before re-evaluation for harvest or thinning. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this tutorial we have seen how we can work some stand level info up and start to use that information to make decisions. As the package develops, I hope that this can then blend other higher level operations such as growth and yield. "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.6.2",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
